package com.rykj.util;


import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UploadManager;

import org.json.JSONException;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import androidx.annotation.Keep;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;


public class QiniuToken2UrlFunc implements Func1<String, Observable<Map<String, String>>> {

    List<InputInfo> mInputInfos;

    UploadManager mUploadManager;

    public QiniuToken2UrlFunc(List<InputInfo> inputInfos) {
        Configuration config = new Configuration.Builder().chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认256K
                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认512K
                .connectTimeout(10) // 链接超时。默认10秒
                .responseTimeout(60).build(); // 服务器响应超时。默认60秒
        // .recorder(recorder)  // recorder分片上传时，已上传片记录器。默认null
        // .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是
        mUploadManager = new UploadManager(config);
        mInputInfos = inputInfos;
    }

    @Override
    public Observable<Map<String, String>> call(String token) {
        return Observable.create(new Token2UpSubscribe(mInputInfos, token, mUploadManager));
    }


    static class Token2UpSubscribe implements Observable.OnSubscribe<Map<String, String>> {

        List<InputInfo> mInputInfos;

        String token;

        UploadManager mUploadManager;

        public Token2UpSubscribe(List<InputInfo> inputInfos, String token, UploadManager uploadManager) {
            mInputInfos = inputInfos;
            mUploadManager = uploadManager;
            this.token = token;
        }


        @Override
        public void call(Subscriber<? super Map<String, String>> subscriber) {

            Map<String, String> map = new HashMap<>();

            for (int i = 0; i < mInputInfos.size(); i++) {
                InputInfo inputInfo = mInputInfos.get(i);
                File idCardFront = inputInfo.upFile;
                String upName=null;
                if (idCardFront != null) {
                    String suffix = idCardFront.getName().substring(idCardFront.getName().lastIndexOf("."));
                    String name = i + "_" + fromatCurentDate("yyyy_MMdd_HH_mm_ss");

                    ResponseInfo idCardFrontResponseInfo = mUploadManager.syncPut(idCardFront, MD5.md5(name) + suffix, token, null);

                    if (idCardFrontResponseInfo.isOK()) {
                        try {
                            upName = idCardFrontResponseInfo.response.getString("key");
                        } catch (JSONException e) {
                        }
                    }
                } else {
                    upName = null;
                }
                map.put(inputInfo.key, upName);
            }
            subscriber.onNext(map);
        }

        String fromatCurentDate(String formatString) {
            SimpleDateFormat ssf = new SimpleDateFormat(formatString, Locale.getDefault());
            return ssf.format(new Date());
        }

    }

    @Keep
    public static class InputInfo {
        String key;
        File upFile;

        public InputInfo(String key, File upFile) {
            this.key = key;
            this.upFile = upFile;
        }
    }

}
    
 